<?php

/**
 *
 * Copyright (c) 2011 Tomáš Holub (tomas.holub@webadvice.cz)
 * 
 * @author Tomáš Holub (tomas.holub@webadvice.cz)
 * @package OverFishCMS
 */

namespace AdminModule\Forms;

use Nette\Application\UI\Form, Nette\Image;

class GalleryUploadForm extends Form {
    
    public function __construct($parent, $name)
    {
        parent::__construct($parent, $name);

        $this->addHidden("id")
                ->setRequired('ID musí být vyplněn');
        $this->addUpload('photo', 'Forografie:')
                ->setRequired('Titulek musí být vyplněn')
                ->setAttribute("class",  "inp-form");
        $this->addSubmit('send', 'Odeslat');
        $this->onSuccess[] = array($this, 'formSubmited');
    }

    public function formSubmited($form)
    {
        if ($form->isValid()) {

            $values = $form->getValues();
            
            $uploadDir = $this->getPresenter()->getContext()->params["wwwDir"] . "/photos/";
            var_dump($values);
            
            if ($values->id){
                
                $gallery = $this->getPresenter()->repositories->gallery->find($values->id);
                
                $uploadDir .= $gallery->getSlug() . "/";
                @mkdir($uploadDir);
                @mkdir($uploadDir . "original");
                @mkdir($uploadDir . "big");
                @mkdir($uploadDir . "thumb");
               
                $values->photo->move($uploadDir . "original/" . $values->photo->getName());
                $values->photo->move($uploadDir . "big/" . $values->photo->getName());
                $values->photo->move($uploadDir . "thumb/" . $values->photo->getName());
                
                $image = Image::fromFile($uploadDir . "thumb/" . $values->photo->getName());
                $image->resize(250, 250);
                $image->save($uploadDir . "thumb/" . $values->photo->getName());

            }
            
            
            
        }
    }
}

?>
